package com.stylefeng.guns.modular.train.service.impl;

import com.stylefeng.guns.common.persistence.model.Employee;
import com.stylefeng.guns.common.persistence.dao.EmployeeMapper;
import com.stylefeng.guns.modular.system.service.IDeptService;
import com.stylefeng.guns.modular.train.service.IEmployeeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jondai
 * @since 2017-12-21
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IDeptService deptService;

    @Override
    public Employee selectByCode(String empCode) {
        return employeeMapper.selectByCode(empCode);
    }

    @Override
    public List<String> selectEmpCodes(Map<String, Object> condition) {
        List<Employee> employees = this.selectByMap(condition);
        List<String> empCodes = new ArrayList<>();
        employees.forEach(employee -> empCodes.add(employee.getEmpCode()));
        return empCodes;
    }

    @Override
    public void fillExcelImportData(Employee employee, Map<String, List> orgMap) throws Exception {

        String organization = organizationName(employee);

        List org = orgMap.get(organization);

        if(CollectionUtils.isEmpty(org)){
            String msg = employee.getEmpName() + "的组织机构不存在，请在组织机构管理中维护!";
            throw new Exception(msg);
        }else{

            //部门
            if(org.size() == 1){
                employee.setDepartment(org.get(0).toString());
            }
            //部门与处室
            if(org.size() == 2){
                employee.setDepartment(org.get(1).toString());
                employee.setPlace(org.get(0).toString());
            }

            if(org.size() == 3){
                employee.setDepartment(org.get(1).toString());
                employee.setPlace(org.get(2).toString());
                employee.setTeam(org.get(0).toString());
            }
        }
    }

    @Override
    public Set<String> getEmpCode() {
        Set<String> strings = employeeMapper.selectEmpCode();
        return strings;
    }

    /**
     * 获取key：姓名   value: 员工编号
     * 如果姓名重复，逗号分隔员工编号
     * @return
     */
    @Override
    public Map<String, String> getNameAndCode() {
        List<Employee> employees = this.selectList(null);
        Map<String,String> map = new HashMap<>();
        for (Employee employee : employees) {
            if(map.containsKey(employee.getEmpName())){
                String code = map.get(employee.getEmpName());
                code += ","+code;
                map.put(employee.getEmpName(),code);
            }else{
                map.put(employee.getEmpName(),employee.getEmpCode());
            }
        }
        return map;
    }

    @Override
    public void cleanData(String table) {
        employeeMapper.cleanData(table);
    }


    private String organizationName(Employee employee){
        String organization = "";
        if(!StringUtils.isEmpty(employee.getDeptName())){
            organization = employee.getDeptName();

            if(!StringUtils.isEmpty(employee.getPlaceName())){
                organization += ","+employee.getPlaceName();

                if(!StringUtils.isEmpty((employee.getTeamName()))){
                    organization += ","+employee.getTeamName();
                }
            }
        }
        return organization;
    }


}
